package com.mf.service.authority.impl;

import com.mf.common.sso.LoginContext;
import com.mf.dao.authority.ResourceInfoDao;
import com.mf.dao.authority.RoleResourceRelationDao;
import com.mf.dao.authority.UserRoleRelationDao;
import com.mf.domain.authority.ResourceInfo;
import com.mf.domain.authority.RoleResourceRelation;
import com.mf.domain.authority.UserRoleRelation;
import com.mf.domain.general.tree.MenuTree;
import com.mf.service.authority.ResourceInfoService;
import com.mf.service.framework.GenericService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 资源信息Service层实现类
 * User: zhaoming
 * DateTime: 2016-07-14
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class ResourceInfoServiceImpl extends GenericService<ResourceInfo> implements ResourceInfoService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ResourceInfoDao resourceInfoDao;
    @Resource
    private UserRoleRelationDao userRoleRelationDao;
    @Resource
    private RoleResourceRelationDao roleResourceRelationDao;

    /**
     * 根据主键删除资源信息
     * @param resourceInfo 资源信息Domain
     */
    @Transactional
    public boolean deleteByPrimaryKey(ResourceInfo resourceInfo){
        resourceInfo = resourceInfoDao.selectByPrimaryKey(resourceInfo);

        ResourceInfo resource = new ResourceInfo();
        resource.setParentResourceCode(resourceInfo.getResourceCode());
        List<ResourceInfo> resourceInfos = resourceInfoDao.selectByCondition(resource);
        if(CollectionUtils.isNotEmpty(resourceInfos)){
            throw new RuntimeException("请先删除该目录下的所有子节点");
        }

        //删除与当前角色编码相关的资源信息
        RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
        roleResourceRelation.setResourceCode(resourceInfo.getResourceCode());
        List<RoleResourceRelation> roleResourceRelations = roleResourceRelationDao.selectByCondition(roleResourceRelation);
        if(CollectionUtils.isNotEmpty(roleResourceRelations)){
            roleResourceRelationDao.deleteByPrimaryKeys(roleResourceRelations);
        }
        return resourceInfoDao.deleteByPrimaryKey(resourceInfo);
    }

    /**
     * 查询资源信息列表(树形)
     * @param resourceInfo 资源信息Domain
     * @return 资源信息列表
     */
    public List<ResourceInfo> treeGirdQuery(ResourceInfo resourceInfo){
        List<ResourceInfo> allResourceInfo = new ArrayList<>();

        resourceInfo.setParentResourceCode("ROOT");
        //查询根目录的子节点
        List<ResourceInfo> resourceInfos = resourceInfoDao.treeGirdQuery(resourceInfo);
        for(ResourceInfo firstResourceInfo : resourceInfos){
            firstResourceInfo.setLevel("0");
            firstResourceInfo.setExpanded(false);
            firstResourceInfo.setParent("");
            firstResourceInfo.setLoaded(true);
            if(firstResourceInfo.getResourceType() == 1){
                firstResourceInfo.setIsLeaf(false);
            }else{
                firstResourceInfo.setIsLeaf(true);
            }
            allResourceInfo.add(firstResourceInfo);

            //二级菜单
            resourceInfo = new ResourceInfo();
            resourceInfo.setParentResourceCode(firstResourceInfo.getResourceCode());
            List<ResourceInfo> secondResourceInfoList = resourceInfoDao.treeGirdQuery(resourceInfo);
            for(ResourceInfo secondResourceInfo : secondResourceInfoList){
                secondResourceInfo.setLevel("1");
                secondResourceInfo.setExpanded(false);
                secondResourceInfo.setParent(firstResourceInfo.getId().toString());
                if(secondResourceInfo.getResourceType() == 1){
                    secondResourceInfo.setIsLeaf(false);
                }else{
                    secondResourceInfo.setIsLeaf(true);
                }
                secondResourceInfo.setLoaded(true);
                allResourceInfo.add(secondResourceInfo);

                //三级菜单
                resourceInfo = new ResourceInfo();
                resourceInfo.setParentResourceCode(secondResourceInfo.getResourceCode());
                List<ResourceInfo> threeResourceInfoList = resourceInfoDao.treeGirdQuery(resourceInfo);
                for(ResourceInfo threeResourceInfo : threeResourceInfoList){
                    threeResourceInfo.setLevel("2");
                    threeResourceInfo.setExpanded(true);
                    threeResourceInfo.setParent(secondResourceInfo.getId().toString());
                    if(threeResourceInfo.getResourceType() == 1){
                        threeResourceInfo.setIsLeaf(false);
                    }else{
                        threeResourceInfo.setIsLeaf(true);
                    }
                    threeResourceInfo.setLoaded(true);
                    allResourceInfo.add(threeResourceInfo);
                }
            }

        }
        return allResourceInfo;
    }

    /**
     * 获取资源菜单
     * @return 资源菜单
     */
    public List<MenuTree> selectMenuResource(){

        List<MenuTree> menuTrees = new ArrayList<>();
        //当前用户的资源列表
        Set<String> resources = new HashSet<>();

        String userCode = LoginContext.getLoginUser().getUserCode();
        UserRoleRelation userRoleRelation = new UserRoleRelation();
        userRoleRelation.setUserCode(userCode);
        List<UserRoleRelation> userRoleRelations = userRoleRelationDao.selectByCondition(userRoleRelation);
        for(UserRoleRelation userRole : userRoleRelations){
            //获取当前用户的资源列表
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setRoleCode(userRole.getRoleCode());
            List<RoleResourceRelation> roleResourceRelations =  roleResourceRelationDao.selectByCondition(roleResourceRelation);
            for(RoleResourceRelation roleResource : roleResourceRelations){
                resources.add(roleResource.getResourceCode());
            }
        }
        Map<String, Boolean> map = new HashMap<>();
        for(String resource : resources){
            map.put(resource, true);
        }

        ResourceInfo firstResource = new ResourceInfo();
        firstResource.setParentResourceCode("ROOT");
        //查询根目录的子节点
        List<ResourceInfo> firstResourceInfos = resourceInfoDao.treeGirdQuery(firstResource);
        for(ResourceInfo firstResourceInfo : firstResourceInfos){
            //资源过滤
            if(map.containsKey(firstResourceInfo.getResourceCode())){
                //装载菜单
                MenuTree firstMenu = new MenuTree();
                firstMenu.setId(firstResourceInfo.getId().toString());
                firstMenu.setCode(firstResourceInfo.getResourceCode());
                firstMenu.setName(firstResourceInfo.getResourceName());
                firstMenu.setUrl(firstResourceInfo.getResourceUrl());
                firstMenu.setIcon(firstResourceInfo.getResourceIcon());
                List<MenuTree> secondMenus = new ArrayList<>();

                ResourceInfo secondResource = new ResourceInfo();
                secondResource.setParentResourceCode(firstResourceInfo.getResourceCode());
                List<ResourceInfo> secondResourceInfos = resourceInfoDao.treeGirdQuery(secondResource);
                for(ResourceInfo secondResourceInfo : secondResourceInfos){
                    if(map.containsKey(secondResourceInfo.getResourceCode())){
                        MenuTree secondMenu = new MenuTree();
                        secondMenu.setId(secondResourceInfo.getId().toString());
                        secondMenu.setCode(secondResourceInfo.getResourceCode());
                        secondMenu.setName(secondResourceInfo.getResourceName());
                        secondMenu.setUrl(secondResourceInfo.getResourceUrl());
                        secondMenu.setIcon(secondResourceInfo.getResourceIcon());

                        List<MenuTree> threeMenus = new ArrayList<>();
                        ResourceInfo threeResource = new ResourceInfo();
                        threeResource.setParentResourceCode(secondResourceInfo.getResourceCode());
                        List<ResourceInfo> threeResourceInfos = resourceInfoDao.treeGirdQuery(threeResource);
                        for(ResourceInfo threeResourceInfo : threeResourceInfos){
                            if(map.containsKey(threeResourceInfo.getResourceCode())){
                                MenuTree threeMenu = new MenuTree();
                                threeMenu.setId(threeResourceInfo.getId().toString());
                                threeMenu.setCode(threeResourceInfo.getResourceCode());
                                threeMenu.setName(threeResourceInfo.getResourceName());
                                threeMenu.setUrl(threeResourceInfo.getResourceUrl());
                                threeMenu.setIcon(threeResourceInfo.getResourceIcon());
                                threeMenus.add(threeMenu);
                            }
                        }
                        secondMenu.setMenus(threeMenus);
                        secondMenus.add(secondMenu);
                    }
                }

                firstMenu.setMenus(secondMenus);
                menuTrees.add(firstMenu);
            }
        }
        return menuTrees;
    }

    /**
     * 检查重复数据
     * @return 验证结果
     */
    public Map<String, Object> checkRepeat(ResourceInfo resourceInfo){
        Map<String, Object> map = new HashMap<>();
        List<ResourceInfo> resourceInfos = resourceInfoDao.selectByCondition(resourceInfo);
        if(CollectionUtils.isNotEmpty(resourceInfos)){
            map.put("error", "重复数据");
        }else{
            map.put("ok", "");
        }
        return map;
    }
}
